#include<set>
#include<array>

namespace Connect5
{
    enum class Role
    {
        NONE,
        BLACK, //黑棋执先
        WHITE
    };
    const int max_score = 1<<28; 
    const int min_score = -max_score;

    class ABPlayer //DFS with Alphea Beta
    {
    public:
        ABPlayer();
        ~ABPlayer();
        void set_board(const Role (&board)[16][16]);
        void set_board(int x, int y, Role r);
        void think(int depth); //思考角色r在depth步内的最优解
        void get_next(int &x, int &y); //获取think结果
        void goahead(int x, int y); //前进一步
        void rollback(int x, int y); //
        int black_score() const;
        void clear();
        void show(); //for debug

    private:
        //递归搜索(depth:0-->depth_)计算当前深度的最优解，并返回分值
        int dfs(int depth, int low, int up);
        void update_black_score(int x, int y);
        int init_state();
        void simple_think();
        void release_step_link();
    
    private:
        Role board_[16][16]; //[row][cow], 左上角看作原点(0行,0列)
        bool init_;
        Role role_; //0深度时执棋角色
        int depth_; //整体思考深度
        int x_; //最优解
        int y_; //
        int nbr_map_[16][16]; //board_[i][j]的邻接棋子个数
        int current_black_score_;
        int step_stack_[128][2]; //([i][0], [i][1])
        int step_cnt_;
        int white_;
        int black_;

    private:
        static int dirs_[8][2]; //8个邻居方向
        static int base_; //计分基数
        static int black_score_5[6][6]; //基础5元组分值, [black][white]
    };
}
